from rest_framework import serializers

from home.models import NavCategory, CourseCategory, Banner


class NavCategorySerializer(serializers.ModelSerializer):
    class Meta:
        model = NavCategory
        fields = ['id','name','display_name','weight','external_link']



class CourseCategorySerializer(serializers.ModelSerializer):
    children = serializers.SerializerMethodField()
    class Meta:
        model = CourseCategory
        fields = ['id','name','level',"description","children"]


    def get_children(self,obj):
        """

        :param obj:  模型
        :return:
        """
        print( obj)
        children = CourseCategory.objects.filter(parent=obj)
        if children:
            ser = CourseCategorySerializer(children,many=True)
            return ser.data
        else:
            return []



class BannerSerializer(serializers.ModelSerializer):
    class Meta:
        model = Banner
        fields = ['id','image','link','note']